﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
	public partial interface IService
{
[OperationContract]
string Employee_Add(Employee model ,int ACID);


[OperationContract]
string Employee_Update(Employee model ,int ACID);


[OperationContract]
string EmployeeByEmplID_Delete(Guid EmplID,int ACID);


[OperationContract]
Employee EmployeeByEmplID_Get(Guid EmplID,int ACID );


[OperationContract]
List<Employee> Employee_Get(string sortBy,int ACID);




}
public partial class Service : IService
{
#region  添加Employee表数据
/// <summary>
/// 添加Employee
/// </summary>
/// <param name="model">Employee实体</param>
/// <returns>空或错误</returns>
public string Employee_Add(Employee model,int ACID)
{
if(model.EmplID==null || model.EmplID.Equals(Guid.Empty))
{
return "『员工ID』不能为空！";
}
if(model.IncomeTax==null )
{
return "『工资计算个人所得税计算方法』不能为空！";
}
if(string.IsNullOrEmpty(model.EmpName))
{
return "『姓名』不能为空！";
}
if(string.IsNullOrEmpty(model.EmpCode))
{
return "『员工号』不能为空！";
}
if(string.IsNullOrEmpty(model.DepCode))
{
return "『部门编号』不能为空！";
}
if(string.IsNullOrEmpty(model.shopcode))
{
return "『分支机构代码』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Employee.Count(p=>p.EmplID==model.EmplID)>0)
{
return "主键已存在！";
}
if(ctx.Employee.Count(p=>p.EmpCode==model.EmpCode)>0)
{
return "员工号已存在！";
}
ctx.Employee.InsertOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 添加失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  添加Employee表数据事物用重构
/// <summary>
/// 添加Employee事物用重构
/// </summary>
/// <param name="model">Employee实体</param>
/// <returns>空或错误</returns>
private string Employee_Add(Employee model,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(model.EmplID==null || model.EmplID.Equals(Guid.Empty))
{
return "『员工ID』不能为空！";
}
if(model.IncomeTax==null )
{
return "『工资计算个人所得税计算方法』不能为空！";
}
if(string.IsNullOrEmpty(model.EmpName))
{
return "『姓名』不能为空！";
}
if(string.IsNullOrEmpty(model.EmpCode))
{
return "『员工号』不能为空！";
}
if(string.IsNullOrEmpty(model.DepCode))
{
return "『部门编号』不能为空！";
}
if(string.IsNullOrEmpty(model.shopcode))
{
return "『分支机构代码』不能为空！";
}
if(ctx.Employee.Count(p=>p.EmplID==model.EmplID)>0)
{
return "主键已存在！";
}
if(ctx.Employee.Count(p=>p.EmpCode==model.EmpCode)>0)
{
return "员工号已存在！";
}
ctx.Employee.InsertOnSubmit(model);
return string.Empty;
}
#endregion



#region  修改Employee表数据
/// <summary>
/// 修改Employee
/// </summary>
/// <param name="model">Employee实体</param>
/// <returns>空或错误</returns>
public string Employee_Update(Employee model,int ACID)
{
if(model.EmplID==null || model.EmplID.Equals(Guid.Empty))
{
return "『员工ID』不能为空！";
}
if(model.IncomeTax==null )
{
return "『工资计算个人所得税计算方法』不能为空！";
}
if(string.IsNullOrEmpty(model.EmpName))
{
return "『姓名』不能为空！";
}
if(string.IsNullOrEmpty(model.EmpCode))
{
return "『员工号』不能为空！";
}
if(string.IsNullOrEmpty(model.DepCode))
{
return "『部门编号』不能为空！";
}
if(string.IsNullOrEmpty(model.shopcode))
{
return "『分支机构代码』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Employee.Count(p=>p.EmplID==model.EmplID)!= 1)
{
return "未找到指定记录或记录不唯一！";
}
Employee Employeemodel=ctx.Employee.Single(p => p.EmplID==model.EmplID);
if(model.EmplID!=null || model.EmplID.Equals(Guid.Empty))
{
Employeemodel.EmplID=model.EmplID;
}
if(model.EmpTypeID!=null )
{
Employeemodel.EmpTypeID=model.EmpTypeID;
}
if(!string.IsNullOrEmpty(model.shopcode))
{
Employeemodel.shopcode=model.shopcode;
}
if(!string.IsNullOrEmpty(model.DepCode))
{
Employeemodel.DepCode=model.DepCode;
}
if(!string.IsNullOrEmpty(model.EmpCode))
{
Employeemodel.EmpCode=model.EmpCode;
}
if(!string.IsNullOrEmpty(model.EmpName))
{
Employeemodel.EmpName=model.EmpName;
}
if(!string.IsNullOrEmpty(model.PassWord))
{
Employeemodel.PassWord=model.PassWord;
}
if(!string.IsNullOrEmpty(model.ZJM))
{
Employeemodel.ZJM=model.ZJM;
}
if(!string.IsNullOrEmpty(model.Sex))
{
Employeemodel.Sex=model.Sex;
}
if(model.Birthday!=null )
{
Employeemodel.Birthday=model.Birthday;
}
if(!string.IsNullOrEmpty(model.Stations))
{
Employeemodel.Stations=model.Stations;
}
if(!string.IsNullOrEmpty(model.Mobile))
{
Employeemodel.Mobile=model.Mobile;
}
if(!string.IsNullOrEmpty(model.QQ))
{
Employeemodel.QQ=model.QQ;
}
if(!string.IsNullOrEmpty(model.MSN))
{
Employeemodel.MSN=model.MSN;
}
if(!string.IsNullOrEmpty(model.Degree))
{
Employeemodel.Degree=model.Degree;
}
if(!string.IsNullOrEmpty(model.Academy))
{
Employeemodel.Academy=model.Academy;
}
if(model.Inday!=null )
{
Employeemodel.Inday=model.Inday;
}
if(model.Outday!=null )
{
Employeemodel.Outday=model.Outday;
}
if(model.IncomeTax!=null )
{
Employeemodel.IncomeTax=model.IncomeTax;
}
if(!string.IsNullOrEmpty(model.Native))
{
Employeemodel.Native=model.Native;
}
if(!string.IsNullOrEmpty(model.IdCard))
{
Employeemodel.IdCard=model.IdCard;
}
if(!string.IsNullOrEmpty(model.HomeTel))
{
Employeemodel.HomeTel=model.HomeTel;
}
if(!string.IsNullOrEmpty(model.HomeAddress))
{
Employeemodel.HomeAddress=model.HomeAddress;
}
if(!string.IsNullOrEmpty(model.custom1))
{
Employeemodel.custom1=model.custom1;
}
if(!string.IsNullOrEmpty(model.custom2))
{
Employeemodel.custom2=model.custom2;
}
if(!string.IsNullOrEmpty(model.custom3))
{
Employeemodel.custom3=model.custom3;
}
if(!string.IsNullOrEmpty(model.custom4))
{
Employeemodel.custom4=model.custom4;
}
if(!string.IsNullOrEmpty(model.custom5))
{
Employeemodel.custom5=model.custom5;
}
if(!string.IsNullOrEmpty(model.custom6))
{
Employeemodel.custom6=model.custom6;
}
if(!string.IsNullOrEmpty(model.Remarks))
{
Employeemodel.Remarks=model.Remarks;
}
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 修改失败!错误详细信息：" + ex.Message;
}
}
}
#endregion



#region  删除Employee表ByEmplID数据
/// <summary>
/// 删除EmployeeByEmplID
/// </summary>
/// <param name="EmplID">员工ID</param>
/// <returns>空或错误</returns>
public string EmployeeByEmplID_Delete(Guid EmplID,int ACID)
{
if(EmplID==null || EmplID.Equals(Guid.Empty))
{
return "『员工ID』不能为空！";
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Employee.Count(p=>p.EmplID==EmplID)!=1)
{
return null;
}
Employee model=ctx.Employee.Single(p => p.EmplID== EmplID);
ctx.Employee.DeleteOnSubmit(model);
ctx.SubmitChanges();
return string.Empty;
}
catch (Exception ex)
{
return " 删除失败!错误详细信息：" + ex.Message;
}
}
}
#endregion

#region  删除Employee表ByEmplID数据[事物用重构]
/// <summary>
/// 删除EmployeeByEmplID[事物用重构]
/// </summary>
/// <param name="EmplID">员工ID</param>
/// <returns>空或错误</returns>
private string EmployeeByEmplID_Delete(Guid EmplID,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
if(EmplID==null || EmplID.Equals(Guid.Empty))
{
return "『员工ID』不能为空！";
}
if(ctx.Employee.Count(p=>p.EmplID==EmplID)!=1)
{
return null;
}
Employee model=ctx.Employee.Single(p => p.EmplID== EmplID);
ctx.Employee.DeleteOnSubmit(model);
return string.Empty;
}
#endregion



#region  获取Employee表ByEmplID数据
/// <summary>
/// 获取EmployeeByEmplID
/// </summary>
/// <param name="EmplID">员工ID</param>
/// <returns>当前表</returns>
public Employee EmployeeByEmplID_Get(Guid EmplID,int ACID)
{
if(EmplID==null || EmplID.Equals(Guid.Empty))
{
return null;
}
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if(ctx.Employee.Count(p=>p.EmplID==EmplID)!=1)
{
return null;
}
return ctx.Employee.SingleOrDefault(p=>p.EmplID==EmplID);
}
catch (Exception ex)
{
return null;
}
}
}
#endregion



#region  获取Employee表数据
/// <summary>
/// 获取Employee
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Employee></returns>
public List<Employee> Employee_Get(Expression<Func<Employee, bool>> predicate , string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Employee.ToList();
}
 else 
{
return ctx.Employee.OrderBy(sortBy).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Employee.Where(predicate).ToList();
}
 else 
{
return ctx.Employee.Where(predicate).OrderBy(sortBy).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  获取Employee表数据
/// <summary>
/// 获取Employee
/// </summary>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Employee></returns>
public List<Employee> Employee_Get(string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
return Employee_Get(null,sortBy, ACID);
}
catch (Exception)
{
return null;
}
}
}
#endregion


#region  获取Employee表数据计数
/// <summary>
/// 获取Employee表数据计数
/// </summary>
/// <returns>List<Employee></returns>
public int Employee_GetCount(Expression<Func<Employee, bool>> predicate,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
return ctx.Employee.Count();

}
 else
{
return ctx.Employee.Where(predicate).Count();
}
}
catch 
{
return 0;
}
}
}
#endregion




#region  获取Employee分页表数据
/// <summary>
/// 获取Employee分页表数据
/// </summary>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="sortBy">排序属性名</param>
/// <param name="sortType">排序类型0=升序 1=降序</param>
/// <returns>List<Employee></returns>
public List<Employee> Employee_Getpage(int pageIndex, int pageSize,Expression<Func<Employee, bool>> predicate ,string sortBy,int ACID)
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
if (predicate == null)
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Employee.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Employee.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
else
{
if(string.IsNullOrWhiteSpace(sortBy))
{
return ctx.Employee.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
 else 
{
return ctx.Employee.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
}
}
}
catch (Exception ex)
{
return null;
}
}
}
#endregion


#region  批量修改Employee表数据
/// <summary>
/// 批量修改Employee
/// </summary>
/// <param name="predicate">Expression<Func<Employee, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Employee,Employee>> 更新表达式树</param>
/// <param name="returns">影响记录数</param>
public int Employee_batchUpdate(Expression<Func<Employee, bool>> predicate,Expression<Func<Employee, Employee>> updater,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Employee.Update(predicate, updater);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量修改Employee表数据事物用重构
/// <summary>
/// 批量修改Employee事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Employee, bool>></param> 查找表达式树
/// <param name="updater">Expression<Func<Employee,Employee>> 更新表达式树</param>
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Employee_batchUpdate(Expression<Func<Employee, bool>> predicate,Expression<Func<Employee, Employee>> updater,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Employee.Update(predicate, updater);
return ok;
}
#endregion


#region  批量删除Employee表数据
/// <summary>
/// 批量删除Employee
/// </summary>
/// <param name="predicate">Expression<Func<Employee, bool>></param> 查找表达式树
/// <param name="returns">影响记录数</param>
public int Employee_batchDel(Expression<Func<Employee, bool>> predicate,int ACID)
{
using (TransactionScope ts=new TransactionScope())
{
using(CardsServer.DAL.DataClassesDataContext ctx=new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
{
try
{
int ok = ctx.Employee.Delete(predicate);
ts.Complete();
 return ok;
}
catch (Exception ex)
{
return 0;
}
}
}
}
#endregion


#region  批量删除Employee表数据事物用重构
/// <summary>
/// 批量删除Employee事物用重构
/// </summary>
/// <param name="predicate">Expression<Func<Employee, bool>></param> 查找表达式树
/// <param name="ctx">数据上下文</param>
/// <param name="returns">影响记录数</param>
private int Employee_batchDel(Expression<Func<Employee, bool>> predicate,int ACID, CardsServer.DAL.DataClassesDataContext ctx)
{
int ok = ctx.Employee.Delete(predicate);
return ok;
}
#endregion


}
}

